package com.csh.szhm.http;

import java.util.HashMap;
import java.util.Iterator;

import com.android.volley.Request;

public class HttpRequestManager {
	private static HashMap<String, HashMap<String, Request>> mRequestManager = new HashMap<String, HashMap<String, Request>>();

	/**
	 * 添加请求
	 * 
	 * @param key
	 * @param req
	 * 
	 */
	public static void put(String root, String key, Request req) {
		if (req == null) {
			return;
		}
		// 如果已存在key就删除，取消请求，删除，然后添加
		if (mRequestManager.containsKey(root)) {
			cencalRequest(root, key);
		}
		HashMap<String, Request> re = new HashMap<String, Request>();
		re.put(key, req);
		mRequestManager.put(root, re);
	}

	/**
	 * 消除本页的请求
	 * 
	 * @param root
	 */
	public static void finishActivity(String root) {
		HashMap<String, Request> map = mRequestManager.get(root);
		if (map == null) {
			return;
		}
		Iterator<String> key = map.keySet().iterator();
		while (key.hasNext()) {
			String temp = key.next();
			mRequestManager.get(root).get(temp).cancel();
			mRequestManager.get(root).remove(temp);
		}
		map.clear();
		map = null;
		mRequestManager.remove(root);
	}

	/**
	 * 返回请求
	 * 
	 * @param key
	 */
	public static void cencalRequest(String root, String key) {
		if (mRequestManager.get(root) == null) {
			return;
		}
		if (mRequestManager.get(root).containsKey(key)) {
			mRequestManager.get(root).get(key).cancel();
			mRequestManager.get(root).remove(key);
		}

	}
}
